{template '_header'}
<div class="page-heading">
	<span class='pull-right'>
		{ifp 'merch.clearing.add'}
			<a class="btn btn-primary btn-sm" href="{php echo webUrl('merch/clearing/add')}">生成结算单</a>
		{/if}
		<a class="btn btn-default  btn-sm" href="{php echo webUrl('merch/clearing')}">返回列表</a>
	</span>
    <h2>生成结算单</h2>
</div>


<form id="setform" action="" method="post" class="form-horizontal form-validate">
    <input type="hidden" name="clearno" value="{$item['clearno']}">
<div class="form-group">
    <label class="col-sm-2 control-label must">商户名称</label>
    <div class="col-sm-8">
        {ife 'merch.clearing' $item}
        {if !$id}
        {php echo tpl_selector('merchid',array('required'=>true,'preview'=>false,'text'=>'merchname','type'=>'text','placeholder'=>'商户名称/姓名/手机号','buttontext'=>'选择商户 ', 'items'=>$merch,'url'=>webUrl('merch/clearing/query') ))}
        {else}
        <input type="hidden" name="merchid" value="{$merch['id']}">
        <div class="form-control-static">{$merch['merchname']}</div>
        {/if}
        {else}
        <div class="form-control-static">{$merch['merchname']}</div>
        {/if}
        <input type="hidden" id="isedit" value="{$id}">
    </div>
</div>

<div class="form-group">
    <label class="col-sm-2 control-label must">选择生成时间</label>
    <div class="col-sm-9 col-xs-12">
        {ife 'merch.clearing' $item}
        {if !$id}
        {php echo tpl_daterange('datetime', array('starttime'=>$_GPC['datetime']['start'],'endtime'=>$_GPC['datetime']['end'],'sm'=>true,'placeholder'=>'按结算时间查询'),true);}
        <div class="help-block">请先选择商户,再选择时间! 否则不计算</div>
        {else}
        <div class="form-control-static">{php echo date('Y/m/d H:i',$item['starttime'])} - {php echo date('Y/m/d H:i',$item['endtime'])}</div>
        {/if}
        {else}
        <div class="form-control-static">{php echo date('Y/m/d H:i',$item['starttime'])} - {php echo date('Y/m/d H:i',$item['endtime'])}</div>
        {/if}
    </div>
</div>

    <div class="form-group-title">商城促销</div>
    <div class="form-group">
        <label class="col-sm-2 control-label must">订单商品总额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="goodsprice" class="form-control" readonly required value="{$item['goodsprice']}" />
            {else}
            <div class="form-control-static">{$item['goodsprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">快递金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="dispatchprice" class="form-control" readonly required value="{$item['dispatchprice']}" />
            {else}
            <div class="form-control-static">{$item['dispatchprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">积分抵扣金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="deductprice" class="form-control" readonly required value="{$item['deductprice']}" />
            {else}
            <div class="form-control-static">{$item['deductprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">余额抵扣金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="deductcredit2" class="form-control" readonly required value="{$item['deductcredit2']}" />
            {else}
            <div class="form-control-static">{$item['deductcredit2']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">会员折扣金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="discountprice" class="form-control" readonly required value="{$item['discountprice']}" />
            {else}
            <div class="form-control-static">{$item['discountprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">促销金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="isdiscountprice" class="form-control" readonly required value="{$item['isdiscountprice']}" />
            {else}
            <div class="form-control-static">{$item['isdiscountprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">满减金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="deductenough" class="form-control" readonly required value="{$item['deductenough']}" />
            {else}
            <div class="form-control-static">{$item['deductenough']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">实际支付金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="price" class="form-control" readonly required value="{$item['price']}" />
            {else}
            <div class="form-control-static">{$item['price']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group-title">商户自身促销</div>
    <div class="form-group">
        <label class="col-sm-2 control-label must">商户满减金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="merchdeductenough" class="form-control" readonly required value="{$item['merchdeductenough']}" />
            {else}
            <div class="form-control-static">{$item['merchdeductenough']}</div>
            {/if}
        </div>
    </div>
<hr>
    <div class="form-group">
        <label class="col-sm-2 control-label must">商家获得金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="realprice" class="form-control" readonly required value="{$item['realprice']}" />
            {else}
            <div class="form-control-static">{$item['realprice']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">抽成比例</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <div class="input-group">
                <input type="text" class="form-control" name="payrate" value="{$item['payrate']}" readonly required/><span class="input-group-addon">%</span>
            </div>
            {else}
            <div class="form-control-static">{$item['realpricerate']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">抽成后金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="realpricerate" class="form-control" readonly required value="{$item['realpricerate']}" />
            {else}
            <div class="form-control-static">{$item['realpricerate']}</div>
            {/if}
        </div>
    </div>

    {if $item['status']=='1'||$item['status']=='2'}
    <div class="form-group">
        <label class="col-sm-2 control-label">最终打款金额</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <input type="text" name="finalprice" class="form-control" {if $item['status']=='2'}readonly required{/if} value="{$item['finalprice']}" />
            {else}
            <div class="form-control-static">{$item['finalprice']}</div>
            {/if}
        </div>
    </div>
    {/if}
    {if $item['status']=='2'}
    <div class="form-group">
        <label class="col-sm-2 control-label">结算时间</label>
        <div class="col-sm-8">
            <div class="form-control-static">{php echo date('Y/m/d H:i:s',$item['paytime'])}</div>
        </div>
    </div>
    {/if}

    <div class="form-group">
        <label class="col-sm-2 control-label">备注</label>
        <div class="col-sm-6 col-xs-12">
            {ife 'merch.clearing' $item}
            <textarea name="remark" class="form-control">{$item['remark']}</textarea>
            {else}
            <div class="form-control-static">{$item['remark']}</div>
            {/if}
        </div>
    </div>

{ife 'merch.clearing' $item}
<div class="form-group">
    <label class="col-sm-2 control-label"></label>
    <div class="col-sm-9 col-xs-12">
        {if $item['status']=='0'}
        <input type="hidden" name="status" value="1">
        <input type="submit"  value="确认结算单" class="btn btn-primary" />
        {elseif $item['status']=='1'}
        <input type="hidden" name="status" value="2">
        <input type="submit"  value="点击结算打款" class="btn btn-primary" />
        {elseif $item['status']=='2'}
        {else}
        <input type="submit"  value="提交" class="btn btn-primary" />
        {/if}
    </div>
</div>
{/if}

</form>

<script language='javascript'>
    var satrttime = '';
    require(['bootstrap'], function () {
        $('#myTab a').click(function (e) {
            $('#tab').val($(this).attr('href'));
            e.preventDefault();
            $(this).tab('show');
        });

        var isedit = $("#isedit").val();
        if (isedit > 0) {
            return;
        }
        setInterval(function(){
            var starttime = $("input[name = 'datetime[start]']").val();
            var endtime = $("input[name = 'datetime[end]']").val();
            var merchid = $("input[name = 'merchid']").val();
            if (starttime == window.satrttime){
                return;
            }else{
                $.ajax({
                    url:"{php echo webUrl('merch/statistics/merch/ajax_user_data');}",
                    type:"get",
                    data:{merchid:merchid,starttime:starttime,endtime:endtime},
                    dataType:"json",
                    success:function (data) {
                        $("input[name = 'goodsprice']").val(data.goodsprice);
                        $("input[name = 'dispatchprice']").val(data.dispatchprice);
                        $("input[name = 'deductprice']").val(data.deductprice);
                        $("input[name = 'deductcredit2']").val(data.deductcredit2);
                        $("input[name = 'discountprice']").val(data.discountprice);
                        $("input[name = 'isdiscountprice']").val(data.isdiscountprice);
                        $("input[name = 'price']").val(data.price);
                        $("input[name = 'realprice']").val(data.realprice);
                        $("input[name = 'realpricerate']").val(data.realpricerate);
                        $("input[name = 'deductenough']").val(data.deductenough);
                        $("input[name = 'merchdeductenough']").val(data.merchdeductenough);
                        $("input[name = 'payrate']").val(data.payrate);
                    }
                });
                window.satrttime = starttime;
            }
        },1000);
    });
</script>
{template '_footer'}